package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.pojo.Department;
import cn.wolfcode.rbac.pojo.Employee;
import cn.wolfcode.rbac.pojo.Role;
import cn.wolfcode.rbac.service.DepartmentService;
import cn.wolfcode.rbac.service.EmployeeService;
import cn.wolfcode.rbac.util.QueryObject;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject queryObject) {
        PageInfo<Employee> employeePageInfo = employeeService.selectallpage(queryObject);
        model.addAttribute("pageResult", employeePageInfo);
        model.addAttribute("departments", departmentService.selectall());
//         List<Employee> employees = employeeService.selectall();
//        model.addAttribute("employees", employees );
        return "employee/list";
    }

    @RequestMapping("/input")
    public String input(Integer id, Model model) {
//        if (id!=null){
        Employee employee = employeeService.selectbyid(id);
        model.addAttribute("employee", employee);
//        }
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee) {
        //保存或修改
        employeeService.save(employee);
        return "redirect:/employee/list";
    }

}
